package cn.com.lms.business.exam.service;

import cn.com.lms.business.exam.persistence.entity.QuestionSubject;
import cn.com.lms.business.exam.persistence.entity.QuestionSubjectFile;
import cn.com.lms.business.exam.persistence.repository.QuestionSubjectFileRepository;

import com.bnzj.core.persistence.BaseService;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.persistence.Transient;
import javax.transaction.Transactional;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionSubjectFileService extends BaseService<QuestionSubjectFileRepository, QuestionSubjectFile,Long> {

    public  void deleteBySubjectId(Long subjectId){
         repository.deleteBySubjectId(subjectId);
    }


    public List<QuestionSubjectFile> batchSave(QuestionSubject questionSubject,List<QuestionSubjectFile> questionSubjectFileList){

        if (!CollectionUtils.isEmpty(questionSubjectFileList)) {
            List<QuestionSubjectFile> questionSubjectFiles = questionSubjectFileList.stream().map(item -> {
                QuestionSubjectFile questionSubjectFile = new QuestionSubjectFile();
                questionSubjectFile.setFilePath(item.getFilePath());
                questionSubjectFile.setFileType(Short.valueOf("1"));
                questionSubjectFile.setSubjectId(questionSubject.getId());
                questionSubjectFile.setCreator(questionSubject.getCreator());
                questionSubjectFile.setCreateTime(questionSubject.getCreateTime());
                questionSubjectFile.setCreatorName(questionSubject.getCreatorName());
                questionSubjectFile.setIsDelete(item.getIsDelete()==null?false:item.getIsDelete());
                return questionSubjectFile;
            }).collect(Collectors.toList());
           return super.saveAll(questionSubjectFiles);
        }
        return null;
    }


    public List<QuestionSubjectFile> findQuestionSubjectBySubjectId(Long subjectId){
        QuestionSubjectFile questionSubjectFile = new QuestionSubjectFile();
        questionSubjectFile.setSubjectId(subjectId);
        questionSubjectFile.setIsDelete(false);
        List<QuestionSubjectFile> questionSubjectFileList = super.findAll(questionSubjectFile);
        return  questionSubjectFileList;
    }
}
